# 视图
from django.shortcuts import render
from .models import SingerInfo
from .serializers import *
from rest_framework import generics
from rest_framework.response import Response
# Create your views here.

# 页面渲染
def render_page(request, page_name):
    return render(request, page_name)

# 根据需要返回所有歌手分类
class All_kind(generics.ListAPIView):
    # 把所需数据获取下，在序列化下
    queryset = {}
    serializer_class = SingerSerializers
    # 重新定义get方法
    def get(self, request, *args, **kwargs):
        # get的信息为所有歌手的分类
        info = ['华语男歌手', '华语女歌手', '华语组合', '欧美男歌手', '欧美女歌手', '欧美组合', '日本男歌手', '日本女歌手', '日本组合', '韩国男歌手', '韩国女歌手', '韩国组合', '其他男歌手', '其他女歌手', '其他组合']
        # 返回获取的信息
        return Response(info)


# 根据歌手分类返回该分类下的歌手
class SingerKind(generics.ListAPIView):
    queryset = {}
    serializer_class = SingerSerializers

    def get(self, request, *args, **kwargs):
        # 根据种类返回内容，先请求种类lind
        kind = request.GET.get('kind', '华语男歌手')
        # 对类型判断，如果类型中组合，例如华语组合或者乐队组合，
        if '组合' in kind:
            kind = kind+'&乐队'
        # 分页操作，request下的GET方法获取页码，默认值为第一页
        page_num = int(request.GET.get('page_num', 1))
        # 获取SingerInfo下的objects筛选数据库中的singer_kind为所请求的kind
        queryset = SingerInfo.objects.filter(singer_kind=kind)
        # 把得到的种类信息序列化，
        kinds = SingerKindSerializers(queryset, many=True)
        #有用的数据存放在列表里
        useful = []
        # 对kind里的数据进行遍历
        for i in kinds.data:
            # 对kind里的数据进行遍历的每一个需要歌手id,姓名，图片，总结下来就是msg(字典)，最后把信息添加到列表中
            singer_id = i.get('singer_id')
            singer_name = i.get('singer_name')
            singer_pic = i.get('singer_pic')
            msg = {'singer_id': singer_id, 'singer_name': singer_name, 'singer_pic': singer_pic}
            useful.append(msg)
        #如果页数超过5页，就没有数据了，所以判断下
        if page_num > 5:
            return Response('暂无更多歌手哦，敬请期待！')
        # 返还给外界，歌手的种类和歌手的信息
        info = {'singer_kind': kind, 'info': useful}
        return Response(info)


# 根据歌手id或者歌手姓名返回该歌手对应歌单
class OneSinger(generics.ListAPIView):
    queryset = {}
    serializer_class = SingerSerializers

    def get(self, request, *args, **kwargs):
        # 发出请求获取一个歌手的id,姓名
        id = request.GET.get('singer_id')
        name = request.GET.get('singer_name')
        # 判断符合条件的id
        if id:
            queryset = SingerInfo.objects.filter(singer_id=id)
        #筛选符合条件的name
        if name:
            queryset = SingerInfo.objects.filter(singer_name=name)
        #将获取满足条件的信息序列化
        singer = OneSingerSerializers(queryset, many=True)
        # 在数据库singer中的data[0]获取singer_name
        singer_name = singer.data[0].get('singer_name')
        singer_pic = singer.data[0].get('singer_pic')
        singer_song_list = singer.data[0].get('singer_song_list')
        useful = []
        # 遍历歌手的歌单
        for i in singer_song_list:
            # 获取歌曲的id,名字，时长，碟片，歌曲的链接
            song_id = i.get('song_id')
            song_name = i.get('song_name')
            song_time = i.get('song_time')
            song_album = i.get('song_album')
            song_download_url = i.get('song_download_url')
            # 将所需信息整合
            info = {'song_id': song_id, 'song_name': song_name, 'song_time': song_time, 'song_album': song_album, 'song_download_url': song_download_url}
            useful.append(info)
        # 需要的歌手姓名，歌曲的图片，所属的总曲数，和整合的信息
        data = {'singer_name': singer_name, 'singer_pic': singer_pic, 'song_num': len(useful), 'info': useful}
        return Response(data)


# 根据歌曲id或歌曲名获取歌曲信息
class Songplay(generics.ListAPIView):
    queryset = {}
    serializer_class = SingerSerializers

    def get(self, request, *args, **kwargs):
        id = request.GET.get('song_id')
        name = request.GET.get('song_name')
        queryset = SingerInfo.objects.all()
        singers = SingerSerializers(queryset, many=True)
        # singer的数据
        singers = singers.data
        info = []
        for singer in singers:
            singer_name = singer.get('singer_name')
            singer_song_list = singer.get('singer_song_list')
            for i in singer_song_list:
                song_id = i.get('song_id')
                song_name = i.get('song_name')
                if song_id == id or song_name == name:
                    song_time = i.get('song_time')
                    song_album = i.get('song_album')
                    song_download_url = i.get('song_download_url')
                    msg = {'singer_name': singer_name, 'song_name': song_name, 'song_time': song_time, 'song_album': song_album, 'song_download_url': song_download_url}
                    info.append(msg)
                    # 终止
                    break
        return Response(info)


# 根据歌曲名获取歌曲信息(模糊匹配)
class SearchSong(generics.ListAPIView):
    queryset = {}
    serializer_class = SingerSerializers

    def get(self, request, *args, **kwargs):
        name = request.GET.get('song_name')
        # 进行模糊匹配
        queryset = SingerInfo.objects.all()
        singers = SingerSerializers(queryset, many=True)
        singers = singers.data
        info = []
        for singer in singers:
            singer_name = singer.get('singer_name')
            singer_song_list = singer.get('singer_song_list')
            for i in singer_song_list:
                song_name = i.get('song_name')
                if name in song_name:
                    song_time = i.get('song_time')
                    song_album = i.get('song_album')
                    song_download_url = i.get('song_download_url')
                    msg = {'singer_name': singer_name, 'song_name': song_name, 'song_time': song_time, 'song_album': song_album, 'song_download_url': song_download_url}
                    info.append(msg)
                    break
        # 判断是否得到满足条件的信息，返回相应的状态码
        if len(info) > 0:
            data = {'Status': 200, 'Data': info}
        else:
            data = {'Status': 404, 'Data': '暂无更多歌曲信息哦'}
        return Response(data)


# 按照歌手搜索
class SearchSinger(generics.ListAPIView):
    queryset = {}
    serializer_class = SingerKindSerializers

    def get(self, request, *args, **kwargs):
        singer_name = request.GET.get('singer_name')
        singers = SingerInfo.objects.filter(singer_name__contains=singer_name)
        if singers:
            data = SingerKindSerializers(singers, many=True)
            info = {'Status': 200, 'Data': data.data[:20]}
            return Response(info)
        else:
            info = {'Status': 404, 'Data': '暂无更多歌手信息'}
            return Response(info)